/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.apache.tools.ant;

import java.io.PrintStream;
import java.util.Properties;
import java.util.Vector;

import org.apache.tools.ant.input.DefaultInputHandler;
import org.apache.tools.ant.input.InputHandler;

import x.java.io.File;

public class Ant {

	/** The default build file name. {@value} */
	public static final String DEFAULT_BUILD_FILENAME = "build.xml";

	/** Our current message output status. Follows Project.MSG_XXX. */
	private int msgOutputLevel = Project.MSG_VERBOSE;

	/** File that we are using for configuration. */
	private File buildFile; /* null */

	/** Stream to use for logging. */
	//public InputStream in = System.in;
	// private static PrintStream out = System.out;
	public PrintStream out = System.out;

	/** Stream that we are using for logging error messages. */
	// private static PrintStream err = System.err;
	public PrintStream err = System.err;

	/** The build targets. */
	private Vector targets = new Vector();

	/** Set of properties that can be used by tasks. */
	private Properties definedProps = new Properties();

	/** Names of classes to add as listeners to project. */
	private Vector listeners = new Vector(1);

	/** File names of property files to load on startup. */
	private Vector propertyFiles = new Vector(1);

	/** Indicates whether this build is to support interactive input */
	//private boolean allowInput = false;

	/** keep going mode */
	private boolean keepGoingMode = false;

	private BuildLogger logger;
	
	public Ant() {
	}

	public void setLogger(BuildLogger logger) {
		this.logger = logger;
	}

	public void build(String file, String target, ClassLoader cl) throws BuildException {
		build(new File(file), target, cl);
	}
	
	public void build(File file, String target, ClassLoader cl) throws BuildException {

		final Project project = new Project();
		project.setCoreLoader(cl);

		Throwable error = null;

		try {
			buildFile = file;
			
			addBuildListeners(project);
			addInputHandler(project);

			//if (allowInput) {
			//	project.setDefaultInputStream(in);
			//}
			// System.setIn(new DemuxInputStream(project));
			// System.setOut(new PrintStream(new DemuxOutputStream(project,
			// false)));
			// System.setErr(new PrintStream(new DemuxOutputStream(project,
			// true)));

			project.fireBuildStarted();

			project.init();

			project.setUserProperty(MagicNames.ANT_FILE, buildFile
					.getAbsolutePath());

			project.setKeepGoingMode(keepGoingMode);

			ProjectHelper.configureProject(project, buildFile);

			//
			if (target == null) {
				if (project.getDefaultTarget() != null) {
					targets.addElement(project.getDefaultTarget());
				}
			} else {
				targets.addElement(target);
			}

			project.executeTargets(targets);

		} catch (Throwable e) {
			error = e;
			// throw e;
		} finally {
			project.fireBuildFinished(error);

		}
	}

	private void addBuildListeners(Project project) {
		if (logger == null) {
			logger = createLogger();
		}
		project.addBuildListener(logger);

	}

	private void addInputHandler(Project project) throws BuildException {
		InputHandler handler = new DefaultInputHandler();
		project.setInputHandler(handler);
	}

	private BuildLogger createLogger() {
		BuildLogger logger = new DefaultLogger();

		logger.setMessageOutputLevel(msgOutputLevel);
		logger.setOutputPrintStream(out);
		logger.setErrorPrintStream(err);
		logger.setEmacsMode(false);

		return logger;
	}
}
